const app = getApp();
const config = require('../../../utils/config.js');
Page({
  data: {
    phone: '', //手机号
    code: '', //验证码
    iscode: 123456, //用于存放验证码接口里获取到的code
    codename: '获取验证码',
    isPhone:false
  },
 
  //获取input输入框的值 
  getPhoneValue: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getCodeValue: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  getCode: function() {
    var a = this.data.phone;
    var _this = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == ""){
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    }else if(!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      console.log(this);
      this.setData({
        isPhone:true
      })
      var num = 61;
      var timer = setInterval(function () {
        num--;
        if (num <= 0) {
          clearInterval(timer);
          _this.setData({
            codename: '重新发送',
            disabled: false
          })
        } else {
          _this.setData({
            codename: num + "s"
          })
        }
      }, 1000)
    }
  }, 
  //获取验证码 
  getVerificationCode() {
    this.getCode();
    var _this=this;
   if(this.data.isPhone==true){
    //  console.log(this);
     _this.setData({
       disabled: true
     })
   }
    
  },
   //提交表单信息  
  save: function(res) {
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (this.data.code == "") {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (this.data.code != this.data.iscode) {
      wx.showToast({
        title: '验证码错误',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      wx.setStorageSync('phone', this.data.phone);
      // console.log(this.getInfo);
      // this.getInfo();
      var that = this;
      wx.showLoading({
        title: '登录中',
      })

      app.globalData.userInfo = res.detail.userInfo,

        console.log(app.globalData.userInfo);
      var userInfo = res.detail;
      wx.request({
        url: config.login,
        method: 'POST',
        data: {
          code: app.globalData.code,
          userInfo: userInfo,
          appId: 'wxe9821d2f8105f916',
          secret: '6759acd91c339a50ca17441766e7ed85'
        },
        success: function (res) {
          if (app.globalData.token == '') {
            app.globalData.token = res.data.data.token;
            app.globalData.isLogin = true;
          }
          wx.hideLoading();
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }
  }
})